package com.seeyon.apps.ddkk.form.workflowevent;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.seeyon.apps.ddkk.manager.KkqxbManager;
import com.seeyon.apps.kk.form.workflowevent.KkBaseExtendPlug;
import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.cap4.form.bean.FormTableBean;
import com.seeyon.ctp.common.constants.ApplicationCategoryEnum;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.organization.bo.V3xOrgLevel;
import com.seeyon.ctp.workflow.event.WorkflowEventData;
import com.seeyon.ctp.workflow.event.WorkflowEventResult;
import com.seeyon.kk.utils.ValueUtils;
import com.seeyon.apps.ddkk.manager.DDKkDistributorManger;
public class DDKkDistributorBeforeStartExtendPlug extends KkBaseExtendPlug {
	private static final Log log = LogFactory.getLog(DDKkDistributorBeforeStartExtendPlug.class);
	
	
	private KkqxbManager kkqxbManager;
	private DDKkDistributorManger DDKkDistributorManger;
	

	public DDKkDistributorManger getDDKkDistributorManger() {
		return DDKkDistributorManger;
	}

	public void setDDKkDistributorManger(DDKkDistributorManger dDKkDistributorManger) {
		DDKkDistributorManger = dDKkDistributorManger;
	}

	public void setKkqxbManager(KkqxbManager kkqxbManager) {
		this.kkqxbManager = kkqxbManager;
	}

	public V3xOrgLevel getLevelByCode(Long accountId, String property, Object feildvalue) {
		return null;
	}


	@Override
	public String getId() {
		// TODO Auto-generated method stub
		return "dDkkDistributorBeforeStartExtendPlug";
	}

	@Override
	public String getLabel() {
		// TODO Auto-generated method stub
		return "Dd创建经销商发起前校验";
	}

	public ApplicationCategoryEnum getAppName() {
		return ApplicationCategoryEnum.form;
	}

	

	//获取三年后的时间
	public static Date getYear(Date today) {
		Calendar cale =  Calendar.getInstance();
		cale.setTime(today);
		cale.add(Calendar.YEAR, 3); 
			return cale.getTime();
		 
	}
	
	
	// 发起前事件
	public WorkflowEventResult onBeforeStart(WorkflowEventData data) {
		WorkflowEventResult result = new WorkflowEventResult();
//		Map<String, Object> formdata = data.getBusinessData();
		FormBean formBean = (FormBean) data.getBusinessData().get("formBean");
		FormDataMasterBean formDataMasterBean = (FormDataMasterBean) data.getBusinessData().get("formDataBean");
		FormTableBean formData = formDataMasterBean.getFormTable();// 获取表单bean对象
		//获取DD经销商审批表bean
		FormTableBean Ddlc = formBean.getMasterTableBean();
		log.info("ddkk:----"+"邮箱校验开始");
		try {
			//邮箱手机号码格式校验
			String checkResult=this.DDKkDistributorManger.mailAndPhoneCheck(formBean, formDataMasterBean);
			if(!"".equals(checkResult)){
				result.setAlertMessage(checkResult);
				return result;
			}
		} catch (Exception e1) {
			log.error("ddkk邮箱手机号校验", e1);
			result.setAlertMessage("邮箱手机号校验异常,不能发起;请联系管理员!");
			return result;
		}
		log.info("ddkk:----"+"DD经销商审批表发起前校验开始");
		 try{
			//DD经销商审批表发起前校验
			Map<String, Object> startJiaoyan = this.DDKkDistributorManger.StartJiaoyan(formBean, formDataMasterBean);
			if(!MapUtils.getBooleanValue(startJiaoyan, "result")){
				result.setAlertMessage(MapUtils.getString(startJiaoyan, "msg"));
				return result;
			}
		} catch (Exception e) {
			log.error("ddkk发起前事件", e);
			result.setAlertMessage("经销商名称、产品线、DD有效期校验异常,不能发起;请联系渠道运营部门!");
			return result;
		}
		log.info("DDKK:DD生成启信宝经销商关键人员发起前事件开始==================");
		String companyName = getString(formDataMasterBean, formBean, "经销商名称");
		for (FormTableBean subTable : formBean.getTableList()) {
			if (subTable.getDisplay().contains("关键人员简历")) {
				formDataMasterBean.getSubData(subTable.getTableName()).remove(0);
			}
		}
		try{
			// 根据经销商名称查询启信宝关键人员并插入底表
			this.kkqxbManager.saveEmployees(companyName, formBean, formDataMasterBean);
		} catch (Exception e) {
			log.error("ddkk发起前事件", e);
			result.setAlertMessage("启信宝模块异常,不能发起;请联系管理员!");
			return result;
		}
		try{
			// 插入默认关键人员到底表并将数据ID挂载到表单上
			this.kkqxbManager.saveDefaultEmployees(companyName, formBean, formDataMasterBean);
		} catch (Exception e) {
			log.error("ddkk发起前事件", e);
			result.setAlertMessage("生成默认关键人员异常,不能发起;请联系管理员!");
			return result;
		}
		try{
			//DD专员排班人员分配
			this.DDKkDistributorManger.HccPaiban(formBean, formDataMasterBean);
		} catch (Exception e) {
			log.error("ddkk发起前事件", e);
			result.setAlertMessage("DD专员排班异常,不能发起;请联系管理员!");
			return result;
		}
		try{
			//发起前校验通过后创建DD经销商账号
			this.DDKkDistributorManger.CreateDistributor(formBean, formDataMasterBean);
		} catch (Exception e) {
			log.error("ddkk发起前事件", e);
			result.setAlertMessage("创建经销商账号并发送邮件异常,不能发起;请联系管理员!");
			return result;
		}
		log.info("DDKK:DD生成启信宝经销商关键人员发起前事件结束==================");
		return null;
	}

	public List<Map> getRoomList(FormBean formBean) throws BusinessException, Exception {

		return null;
	}

	// 处理前事件
	public WorkflowEventResult onBeforeFinishWorkitem(WorkflowEventData data) {
		Map<String, Object> businessData = data.getBusinessData();
		FormDataMasterBean formBean = (FormDataMasterBean) businessData.get("formDataBean");
		FormTableBean formData = formBean.getFormTable();// 获取表单bean对象
		Long id = data.getSummaryId();
		businessData.put("SummaryId", id);
		String findSql = this.freemarkFlowManager.fltTemplateIntoString("invitation/InvitionAgree.ftl", businessData);
		List<Map<String, Object>> queryForList = this.kkJdbcTemplate.queryForList(findSql);
		for (Map<String, Object> map : queryForList) {
			if (!ValueUtils.getConfig().getString("kk.DdJxsMj").equals(MapUtils.getString(map, "Meiju"))) {
				WorkflowEventResult er = new WorkflowEventResult();
				er.setAlertMessage("状态未更新不允许提交");
				return er;
			}
		}
		return null;
	}
	

	private String getString(FormDataMasterBean formDataMasterBean, FormBean formBean, String display){
		try{
			return formDataMasterBean.getFieldValue(formBean.getMasterTableBean()
					.getFieldBeanByDisplay(display).getName()).toString();
		} catch(Exception e) {
			return null;
		}
	}
}
